from BaseController import BaseController
from Common.HIT import HIT
from Utility import ParameterKey
from Utility import IsEmptyString
from Utility import ExternalQuestionForm

class ShowQuestionController(BaseController):
    def get(self):
        """
        this is called by amazon to show worker the question
        return html format form data from QuestionForm by form id.
        identify user in preview mode or working mode. Please refer external question document
        """
        
        q_id = self.request.get(ParameterKey.QUESTION_ID)
        if IsEmptyString(q_id):
            self.response.out.write("The question id is none!")
            return;
        try:
            id = int(q_id);
        except ValueError:
            self.response.out.write("The question id is none!")
            return;
        # load HIT according to id
        # q_id is local hit id
        h = HIT.get_by_id(id)
        # hit is not found, return null
        if h is None:
            self.response.out.write("The question does not exit!")
            return;    
        
        questionContent = h.QuestionForm
        
        asg_id = self.request.get(ParameterKey.HIT_PREVIEW)
        if (not IsEmptyString(asg_id)) and (asg_id != "ASSIGNMENT_ID_NOT_AVAILABLE"):
            # display question in accepted mode
            # ToDo: append form header and submit button
            questionContent = ExternalQuestionForm.AcceptedHeader + questionContent
            questionContent += "<input type='hidden' name='" + ParameterKey.HIT_PREVIEW + "' value='" + asg_id + "'/>"
            questionContent += ExternalQuestionForm.AcceptedFooter
        
        self.response.out.write(questionContent)
        
        
